
# coding: utf-8

# In[13]:

import pandas as pd
import numpy as np


# In[15]:

# read titanic_train.csv
titanic_survival = pd.read_csv('titanic_train.csv')

# print fist 5 rows
print(titanic_survival.head())


# In[64]:

# get page column
# NaN stands for "not a number", to indicate a missing data
age = titanic_survival['Age']
print(age[:10])


# In[63]:

# we can use the pandas.isnull() function which takes a pandas series
# and returns a series of True and False values
age_is_null = pd.isnull(age)
print(age_is_null[:10])


# In[62]:

age_null_true = age[age_is_null]
print(age_null_true[:10])


# In[21]:

# print the number of age_null_count
age_null_count = len(age_null_true)
print(age_null_count)


# In[22]:

# The result of this is that mean_age would be NaN.
# This is because any calculations we do with a null value also result in a null value
mean_age = sum(titanic_survival['Age']) / len(titanic_survival['Age'])
print(mean_age)


# In[24]:

good_ages = titanic_survival['Age'][age_is_null == False]
correct_mean_age = sum(good_ages) / len(good_ages)
print(correct_mean_age)


# In[25]:

# missing data is so common that many pandas methods automatically filter for it
correct_mean_age = titanic_survival['Age'].mean()
print(correct_mean_age)


# In[32]:

# mean fare for each class
passgenger_class = [1, 2, 3]
fare = {}
for each_class in passgenger_class:
    mean_fee = titanic_survival['Fare'][titanic_survival['Pclass']==each_class].mean()
    fare[each_class] = mean_fee
print(fare)


# In[34]:

pclass_survived = titanic_survival.    pivot_table(index='Pclass', values='Survived', aggfunc=np.mean)
print(pclass_survived)


# In[35]:

pclass_age = titanic_survival.pivot_table(index='Pclass', values=['Age', 'Survived'])
print(pclass_age)


# In[38]:

name_row_19 = titanic_survival.loc[19]['Name']
print(name_row_19)


# In[46]:

new_titanic_survival = titanic_survival.sort_values('Age',ascending=False)
# print(new_titanic_survival)


# In[54]:

new_titanic_survival = new_titanic_survival.reset_index(level='Pclass', drop=True)
print(new_titanic_survival.loc[0:2])


# In[55]:

def hundredth_row(column):
    # Extract the hundredth item
    hundredth_item = column.iloc[99]
    return hundredth_item
hundredth_row = titanic_survival.apply(hundredth_row)
print(hundredth_row)


# In[59]:

# By passing in the axis = 1 argument, we can use the DataFrame.apply method
# to iterate rows instead of columns
def which_class(row):
    pclass = row['Pclass']
    if pd.isnull(pclass):
        return 'Unknown'
    elif pclass == 1:
        return 'First Class'
    elif pclass == 2:
        return 'Second Class'
    elif pclass == 3:
        return 'ThirdClass'
classes = titanic_survival.apply(which_class, axis=1)
print(classes[:10])


# In[58]:

def is_minor(row):
    if row['Age'] < 18:
        return True
    else:
        return False
minors = titanic_survival.apply(is_minor, axis=1)
print(minors[:5])


# In[60]:

def generate_age_label(row):
    age = row['Age']
    if pd.isnull(age):
        return 'Unknown'
    elif age < 18:
        return 'minor'
    else:
        return 'adult'
age_label = titanic_survival.apply(generate_age_label, axis=1)
print(age_label[:5])


# In[61]:

titanic_survival['age_label'] = age_label
age_group_survival = titanic_survival.pivot_table(index='age_label', values='Survived')
print(age_group_survival)





